package com.xxrl.shop.service;

import com.xxrl.shop.domain.ProductType;
import com.xxrl.shop.repository.ProductTypeRepository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author mis
 */
@Service
public class ProductTypeService {

    private final ProductTypeRepository productTypeRepository;

    public ProductTypeService( ProductTypeRepository productTypeRepository) {
        this.productTypeRepository = productTypeRepository;
    }

    public List<ProductType> getAllTypes() {
        return productTypeRepository.findAll();
    }

    public ProductType createType(String type) {
        ProductType productType = new ProductType();
        productType.setName(type);
        productType = productTypeRepository.save(productType);
        return productType;
    }

    public void ensureExist(String type) {
        if (!productTypeRepository.existsByName(type)) {
            createType(type);
        }
    }


    public void deleteType(String id) {
        ProductType productType = new ProductType(id);
        productTypeRepository.delete(productType);
    }
}
